﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using d = SponsorshipManager.Domain;
using SponsorshipManager.Database;

namespace SponsorshipManager.BusinessLayer
{
    public class User : PersonDTO
    {
        public static bool AddUser(int PersonId, string Username, string Password, int OrgId, out string ErrorMessage)
        {
            StoredProcedure sp = Database.Database.CurrentDB.StoredProcedure();
            sp.Name = "uspAddUser";
            sp.AddParameter("OrgId", DbType.Int32, PersonId);
            sp.AddParameter("Username", DbType.String, PersonId);
            sp.AddParameter("Password", DbType.Int32, PersonId);
            int ret;
            ErrorMessage = "";
            try
            {
                ret = sp.ExecuteNoQuery();
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;
                return false;
            }
            return true;

        }

        public static User Login(string Username, string Password, out string ErrorMessage)
        {
            StoredProcedure sp = DatabaseAdapter.Database.StoredProcedure();
            sp.Name = "uspAuthenticate";
            sp.AddParameter("Username", DbType.String, Username);
            sp.AddParameter("Password", DbType.String, Password);
            sp.AddParameter("UserId", DbType.Int32);
            sp.AddParameter("Token", DbType.String);
            int ret;
            ErrorMessage = "";
            try
            {
                ret = sp.ExecuteNoQuery();
            }
            catch(Exception e)
            {
                ErrorMessage = e.Message;
                return null;
            }


            if (ret == 1)
            {
                int UserId = (int)sp.GetParameter("UserId").Value;
                string Token = sp.GetParameter("Token").Value.ToString();
                User u = new User();
                u.UserId = UserId;
                u.Token = Token;
                u.Load();
                //AuthenticatedUsers.Add(u);
                return u;
            }
            else
            {
                return null;
            }
        }



        public bool NeedsToSave { get; set; }

        public override bool Load()
        {
            StoredProcedure sp = DatabaseAdapter.Database.StoredProcedure();
            sp.Name = "uspGetUser";
            sp.AddParameter("UserId", DbType.Int32, UserId);
            sp.Token = this.Token;
            int ret = sp.Execute();
            if (ret == 1 )
            {
                DataRow dr = sp.QueryResult.Rows[0];
                Username = dr["username"].ToString();
                /*PersonId = (int)dr["PersonId"];*/
                UserLevel = (int)dr["UserLevel"];
            }
            base.Load();
            return ret==1;
        }

        public override bool Save() 
        {
            StoredProcedure sp = DatabaseAdapter.Database.StoredProcedure();
            sp.AddParameter("UserId", DbType.Int32, UserId);
            sp.AddParameter("Username", DbType.String, Username);
            sp.AddParameter("UserLevel", DbType.Int32, UserLevel);
            sp.Name = "uspSaveUser";
            sp.Token = Token;
            int ret = sp.ExecuteNoQuery();
            return (ret == 1);
        }

        public override string ToString() {
            string nl = Environment.NewLine;
            string s = String.Concat(
                base.ToString(), nl,
                "User", nl, "---------------------------", nl,

                "User Id   : ", UserId, nl,
                "Username  : ", Username, nl,
                "UserLevel : ", UserLevel, nl,
                "Token     : ", Token.Length > 0
            );
            return s;

        }
   
    }
}
